package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ActivityPro;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.dao.ActivityMapper;
import com.yami.shop.service.ActivityService;
import com.yami.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, ActivityPro> implements ActivityService {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    @Transactional
    public String saveActivityPro(ActivityPro activityPro) {
        //新增活动商品数据,同时修改产品中的数据
        String result = "success";
        //当没有重复的数据时,新增活动商品
        Long activityId = activityPro.getActivityId();
        ActivityPro activityProd = activityService.getById(activityId);
        if(activityProd==null){
            activityPro.setStatus("0");
            activityPro.setCreateTime(new Date());
            activityService.save(activityPro);

            //获取产品ID
            Long prodId = activityPro.getProdId();
            Product product = productService.getProductByProdId(prodId);
            if(product!=null){
                product.setActivityTimes(activityPro.getActivityTimes());
                product.setActivityPrice(activityPro.getActivityPrice());
                product.setActivityId(activityPro.getActivityId());
                product.setActivityOrderTimes(activityPro.getActivityOrderTimes());
                product.setActivityOrderFlag(activityPro.getActivityOrderFlag());
                product.setActivityTimesFlag(activityPro.getActivityTimesFlag());
                productService.saveOrUpdate(product);
            }
        }else{
            String error ="repeat";
            return error;
        }

        return result;
    }

    @Override
    @Transactional
    public String deleteActivity(ActivityPro activityPro) {
        String result ="success";
        Long  activityId = activityPro.getActivityId();
        Long prodId = activityPro.getProdId();
        ActivityPro activityProd = activityService.getById(activityId);
        Product product =productService.getById(activityProd.getProdId());
        Long acId = null;
        if(activityPro!=null){
            //删除活动列表的数据
            activityService.removeById(activityId);
            //修改产品列表数据
            product.setActivityId(0l);
            product.setActivityPrice(0.00);
            product.setActivityTimesFlag(0);
            product.setActivityTimes(0);
            product.setActivityOrderTimes(0);
            product.setActivityOrderFlag(0);
            productService.saveOrUpdate(product);

        }
        return result;
    }

    @Override
    @Transactional
    public String updateActivity(ActivityPro activityPro) {
        String result = "success";
        activityPro.setUpdateTime(new Date());
        activityService.saveOrUpdate(activityPro);

        Long prodId = activityPro.getProdId();
        Product product =productService.getById(activityPro.getProdId());
        if(product!=null){
            product.setActivityPrice(activityPro.getActivityPrice());
            product.setActivityTimes(activityPro.getActivityTimes());
            productService.saveOrUpdate(product);
        }
        return result;
    }

    @Override
    public Integer getMaxNumber(String userId, Long prodId) {
        return activityMapper.getMaxNumber(userId,prodId) ;
    }

    @Override
    public ActivityPro getActivityProdByProdId(Long prodId) {
        return activityMapper.getActivityProdByProdId(prodId) ;
    }

    @Override
    public Integer getMaxOrderNumber(String userId, Long prodId) {
        return activityMapper.getMaxOrderNumber(userId,prodId);
    }

    @Override
    public IPage<ActivityPro> getActivityProdByShopId(Page<ActivityPro> page, Long shopId) {
        page.setRecords(activityMapper.getActivityProdByShopId(new PageAdapter(page),shopId));
        page.setTotal(activityMapper.countActivityProd(shopId));
        return page;
    }

}
